
$(function() {
	searchEntity();
});

//查询检索列表和左侧我的项目状态
function searchEntity() {
	var searchText = $("#searchText").val();
	var searchStatus = $("#searchStatus").val();
	$.ajax({
	  url: 'project!searchList.action',
	  type: 'post',
	  data: {
	  	"searchText": searchText,
	  	"searchStatus": searchStatus
	  },
	  dataType: 'json',
	  success: function(data) {
		  Interceptor(data);
	  	 // 左侧
	  	 var r = ''; 
	  	 $(data[0].statusList).each(function(i) {
	  	 	r += '<li><a href="javascript:void(0)" onclick="statusHandle(\'' + data[0].statusList[i].split(',')[1] + '\')" >' + data[0].statusList[i].split(',')[0] + '</a></li>';
	  	 });
	  	 $("#navData").html(r);
	     // 列表
	     var resultContent = '<tr>'
					          + '<th scope="col" width="120">注册号</th>'
					          + '<th scope="col">项目名称</th>'
					          + '<th scope="col">项目状态</th>'
					          + '<th scope="col" width="70">注册日期</th>'
					          + '<th scope="col" width="80">操作</th>'
					          + '</tr>';
	     $(data[0].list).each(function(i) {
	     		var statusStr = '';
	     		var operaStr = '';
	     		var trStr = '';
	     		var isSummarizeStr= '';
	     		if(i%2==0) {
	     			trStr = 'class="odd"';
	     		}
	     		var reasonStr='';
	     		if(data[0].list[i].reason!=null && data[0].list[i].reason!='') {
	     			reasonStr = '<a href="javascript:void(0)" onclick="alert(\'' + data[0].list[i].reason + '\')">审核意见</a><br>'
	     		}
	     		if(data[0].list[i].isSummarize=='1') {
	     			isSummarizeStr = '<a href="project_summarize_detail.jsp?id=' + data[0].list[i].id + '">完成总结</a><br>';
	     		}else if(data[0].list[i].isSummarize=='0') {
	     			isSummarizeStr = '<a href="project_summarize.jsp?id=' + data[0].list[i].id + '">完成总结</a><br>';
	     		}
	     		if(data[0].list[i].status=='-1') {
	     			statusStr = '草稿/未填完';
	     			operaStr = '<a href="project_edit.jsp?id=' + data[0].list[i].id 
	     			+ '">编辑</a><br><a href="javascript:void(0)" onclick="delProject(' + data[0].list[i].id + ');">删除</a>';
	     		}else if(data[0].list[i].status=='3') {
	     			statusStr = '驳回';
	     			operaStr = reasonStr + '<a href="project_edit.jsp?id=' + data[0].list[i].id + '">重新编辑</a>';
	     		}else if(data[0].list[i].status=='0') {
	     			statusStr = '待机构管理员审查';
	     			operaStr =  '无';
	     		}else if(data[0].list[i].status=='1' && data[0].list[i].isProvinceManagerAudit=='0' ) {
	     			statusStr = '待省级管理员审查';
	     			operaStr = '无';
	     		}else if( (data[0].list[i].status=='1' && data[0].list[i].isProvinceManagerAudit=='') || (data[0].list[i].status=='1' && data[0].list[i].isProvinceManagerAudit=='1') ) {
	     			statusStr = '审查通过';
	     			operaStr = '<a href="project_report.jsp?id=' + data[0].list[i].id + '">年度报告</a><br><a href="project_version.jsp?id=' + data[0].list[i].id + '">项目变更</a><br>' + isSummarizeStr;
	     		}else if(data[0].list[i].status=='2') {
	     			statusStr = '审查不通过';
	     			operaStr = reasonStr;
	     		}
				resultContent += '<tr ' + trStr +  '>'
                            	+ '<td height="30">' + data[0].list[i].regNumber + '</td>'
                                + '<td><h2><a href="/index/project_detail.jsp?id=' + data[0].list[i].id + '">' + data[0].list[i].name + '</a></h2></td>'
                                + '<td align="center" style="color: red;">' + statusStr + '</td>'
                                + '<td align="center">' + data[0].list[i].createTime.substring(0,10) + '</td>'
                                + '<td align="center">' + operaStr + '</td>'
                                + '</tr>';
		 });
		 
		 if(data[0].list==null || data[0].list=='') {
		 	resultContent += '<tr><td colspan="5" height="30">您还未登记任何项目信息！</td></tr>'
		 }
		 $("#mainData").html(resultContent); 
	  }, 
	  error: function() {
		  //
	  }
    });
}

function delProject(id) {
	if(confirm('您确定要删除么?')) {
		 $.ajax({
			url : 'project!delete.action',
			type : 'post',
			data: {
				"id": id
			},
			dataType: 'html',
			success : function(data) {
				Interceptor(data);
			     if(data!=null && data=='success') {
			    	 searchEntity();
			     }
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				// 保存失败
				alert('error:' + textStatus);
			}
	    });
	}
}

function statusHandle(value){
	$("#searchStatus").val(value);
	searchEntity();
}